//app.js
App({
  baseURL: 'https://try.0791jr.com/app.php?m=App&c=',
  onLaunch: function (options) {
    this.isShare = false;
    if (options.query.other_user_id) {
      // 如果好友分享进入或者群聊分享进入
      options.query.type == 1 && (this.invitation = true)
      this.isShare = true;
      this.other_id = options.query.other_user_id;
    }

    //检测用户的登录态
    wx.checkSession({
      success: (res) => {
        //登录态未过期，并且在本生命周期一直有效
        var value = wx.getStorageSync('userId');
        if (value) {
          this.globalData.userId = value;
          // var car_num = wx.getStorageSync('car_num');
          // if (car_num - 0 > 0) {
          //   this.setBadge(2, car_num)
          // }
        } else {
          //未登录或者本地存储没有信息，重新发起登录
          this.userLogin();
        }
        
      },
      fail: (err) => {
        //登录态过期,重新登录
        this.userLogin();
      }
    })
  },

  //用户登录
  userLogin(callback) {
    //1.获取code
    wx.login({
      success: res => {
        if (res.code) {
          // wx.showLoading({ title: '登录中...', mask: true });
          let data = {code: res.code,};//js_code 
          console.log(this.other_id);
          this.isShare && (data.parent_user_id = this.other_id);
          this.invitation && (data.type = 1);
          //用户点击了授权登录小程序
          //发送登录请求
          wx.request({
            url: this.baseURL + 'Mine&a=wx_login',
            data: data,
            success: res => {
              console.log(res);
              //登录成功
              if (res.data.status == 0) {
                // wx.hideLoading();
                wx.showToast({ title: '对不起，您的账号已禁用', icon: 'none' });
              } else if (res.data.status == 1) {
                // wx.hideLoading();
                const _3rd = res.data['3rd_session']; //_srd
                  // car_num = res.data.car_num; //购物车商品数量,string

                // _3rd存储本地storage、赋值给全局变量userId
                // wx.setStorageSync("userId", _3rd); 
                this.globalData.userId = _3rd; 
                
                //设置tabBarBadge、购物车商品数量存入本地
                // car_num > 0 && this.setBadge(2, car_num + ''); 
                // wx.setStorageSync("car_num", car_num + '');

                //提示登录成功、如果存在回调函数，执行回调
                // wx.showToast({ title: '登录成功', icon: 'none' });
                typeof callback == 'function' && callback();
                this.loginFinished && this.loginFinished(_3rd); 
              }
            },
            fail: err => {
              //登录失败
              wx.hideLoading();
              wx.showToast({ title: '网络错误', icon: 'none', mask: true});
            }
          })
        }
      }
    });
  },

  //去登录页面
  toLogin() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  
  //设置购物车页面的tabBarBadge
  setBadge(index, num) {
    if (num - 0 <= 0) {
      wx.removeTabBarBadge({
        index: index
      });
      wx.removeStorageSync('car_num');
    } else {
      wx.setTabBarBadge({
        index: index,
        text: num
      });
      wx.setStorageSync('car_num', num + '')
    }
    
  },

  //全局信息
  globalData: {
    title: "贴如意", //小程序标题
    userInfo: null, //用户信息
    userId: null, //用户id
    catedid: null, //首页查看更多
    catedname: null, //首页查看名称
    catedType: 0,  //从首页点击查看更多
  }
})